//模拟课程数据
const courses = [{
    id: 1,
    name: "c语言基础",
    hours: 50
},
{
    id: 2,
    name: "javascript",
    hours: 40
},
{
    id: 3,
    name: "HTML&CSS",
    hours: 60
},
{
    id: 4,
    name: "java",
    hours: 70
}
]

//模拟班级数据
const classes = Mock.mock({
    "list|400": [{
        "clId|+1": 1,
        "clname": "JG@clId"
    }]
}).list;

//模拟学生数据
const students = Mock.mock({
    "list|500": [{
        "id|+1": 1,
        "name": "@cname",
        "gender": /[男女]/,
        "age|15-28": 1,
        "address": "@city(true)",
        "phone": /1\d{10}/,
        "className|1": classes.map(it => it.clname),
        "courseName|1": courses.map(it => it.name)
    }]
}).list;

Mock.mock(/^\/course([?][^?]+)?$/, "get", function () {
    console.log("fsadfs")
    return {
        "code": 0,
        "msg": "",
        "count": courses.length,
        "data": courses
    };
});

Mock.mock(/^\/course\/\d+$/, "get", function (opt) {
    const id = _.last(opt.url.split("/"));
    return courses.find(it => it.id == id);
});

Mock.mock(/^\/course\/\d+$/, "put", function (opt) {
    const c = decodeBody(opt.body);
    const id = _.last(opt.url.split("/"));
    const oldc = courses.find(it => it.id == id);
    modifyObj(oldc, c);
    return true;
});

Mock.mock(/^\/course\/\d+$/, "delete", function (opt) {
    const id = _.last(opt.url.split("/"));
    _.remove(courses, item => item.id == id);
    return true;
});

Mock.mock("/course", "post", function (opt) {
    const newCourse = decodeBody(opt.body);
    newCourse.id = Math.max(...courses.map(it => it.id)) + 1;
    courses.push(newCourse);
})


Mock.mock(/\/class\?page=\d+&limit=\d+/, "get", function (opt) {
    const q = query(opt.url);
    console.log(q);
    const result = {
        "code": 0,
        "msg": "",
        "count": classes.length,
        "data": _.chunk(_.orderBy(classes, ['id'], ['desc']), q.limit)[q.page - 1]
    };
    return result;
});

Mock.mock("/class", "get", function (opt) {
    return classes;
});


Mock.mock(/^\/class\/\d+$/, "delete", function (opt) {
    const id = _.last(opt.url.split("/"));
    _.remove(classes, item => item.id == id);
    return true;
});


Mock.mock("/class", "post", function (opt) {
    const newClass = {};
    newClass.id = Math.max(...classes.map(it => it.id)) + 1;
    newClass.name = "JG" + newClass.id;
    classes.push(newClass);
    return newClass;
})

Mock.mock(/^\/student([?][^?]+)?$/, "get", function (opt) {
    const q = query(opt.url);
    console.log(q)
    if (!q.field) {
        q.field = "id";
    }
    if (!q.order) {
        q.order = "desc";
    }
    const result = {
        "code": 0,
        "msg": "",
        "count": students.length,
        "data": _.chunk(_.orderBy(students, [q.field], [q.order]), q.limit)[q.page - 1]
    };
    return result;
});

Mock.mock(/^\/student\/\d+$/, "get", function (opt) {
    const id = _.last(opt.url.split("/"));
    return students.find(it => it.id == id);
});

Mock.mock(/^\/student\/\d+$/, "put", function (opt) {
    const s = decodeBody(opt.body);
    const id = _.last(opt.url.split("/"));
    const olds = students.find(it => it.id == id);
    modifyObj(olds, s);
    return true;
});

Mock.mock("/student", "post", function (opt) {
    const newStu = decodeBody(opt.body);
    newStu.id = Math.max(...students.map(it => it.id)) + 1;
    students.push(newStu);
})

Mock.mock(/^\/student\/\d+$/, "delete", function (opt) {
    const id = _.last(opt.url.split("/"));
    _.remove(students, item => item.id == id);
    return true;
});

function decodeBody(body) {
    const obj = {};
    body.split("&").forEach(item => {
        const [p, v] = item.split("=");
        obj[p] = decodeURIComponent(v);
    })
    return obj;
}

function modifyObj(oldObj, newObj) {
    for (const [p, v] of Object.entries(newObj)) {
        oldObj[p] = v;
    }
}

function query(url) {
    const q = {};
    _.last(url.split("?")).split("&")
        .map(it => it.split("="))
        .forEach(([p, v]) => {
            q[p] = decodeURIComponent(v);
        });
    return q;
}